import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import {FORM_OPTIONS, DATE_FORMAT} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {injectIntl} from './intl';
import {transportType, declarePlace, declareType} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';
import FileUpload from './FileUpload';

const {Description} = DescriptionList;

export class DeclarePanel extends React.PureComponent {
    state = {
        hasOperat: false,
        loading: false,
        data: {},
    }
    componentDidMount() {
        this.props.init();
    }

    omponentWillUnmount() {
        this.isunmount = true;
    }

    onChangeUpload = value => {
        if(!this.state.hasOperat)
            this.setState({
                hasOperat: true
            });
        this.props.updateAttachments(value);
    }

    onSubmit = () => {
        this.setState({
            loading: true,
        });
        this.props.onDeclareSubmit(this.props.id, this.state.data).then(() => {
            if(this.isunmount)
                this.setState({loading: false});
        });
    }
    
    render() {
        const {formatMessage} = this.props.intl;
        const {declareAttachments = []} = this.props.detail;
        const Yes = formatMessage({
            id: 'declare.yes',
            defaultMessage: '是'
        });
        const No = formatMessage({
            id: 'declare.no',
            defaultMessage: '否'
        });
        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'declare.text.title',
                        defaultMessage: '备件发运单详情'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'declare.text.brandCode',
                            defaultMessage: '品牌编号'
                        })}>{this.props.detail.brandCode}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.dealerCode',
                            defaultMessage: '总代编号'
                        })}>{this.props.detail.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.code',
                            defaultMessage: '编号'
                        })}>{this.props.detail.code}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.invoicedDate',
                            defaultMessage: '开票日期'
                        })}>{formatDateTime(this.props.detail.invoicedDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.partInvoiceCode',
                            defaultMessage: '备件箱单发票编号'
                        })}>{this.props.detail.partInvoiceCode}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.shipmentType',
                            defaultMessage: '发运方式'
                        })}>{conventEnumValueToString(transportType, this.props.detail.shipmentType)}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.origin',
                            defaultMessage: '起运地'
                        })}>{this.props.detail.origin}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.destination',
                            defaultMessage: '目的地'
                        })}>{this.props.detail.destination}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.shipmentDay',
                            defaultMessage: '出运天数'
                        })}>{this.props.detail.shipmentDay}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.expectDepartedDate',
                            defaultMessage: '预计离港日期'
                        })}>{formatDateTime(this.props.detail.expectDepartedDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.actualDepartedDate',
                            defaultMessage: '实际离港日期'
                        })}>{formatDateTime(this.props.detail.actualDepartedDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.expectArrivalDate',
                            defaultMessage: '预计到港日期'
                        })}>{formatDateTime(this.props.detail.expectArrivalDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.actualArrivalDate',
                            defaultMessage: '实际到港日期'
                        })}>{formatDateTime(this.props.detail.actualArrivalDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.lcCode',
                            defaultMessage: '信用证编号	'
                        })}>{this.props.detail.lcCode}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.declarePlace',
                            defaultMessage: '报关地点'
                        })}>{conventEnumValueToString(declarePlace, this.props.detail.declarePlace)}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.declareType',
                            defaultMessage: '报关类型'
                        })}>{conventEnumValueToString(declareType, this.props.detail.declareType)}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.shortContainerCount',
                            defaultMessage: '矮柜数量'
                        })}>{this.props.detail.shortContainerCount}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.standardContainerCount',
                            defaultMessage: '标准柜数量'
                        })}>{this.props.detail.standardContainerCount}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.highContainerCount',
                            defaultMessage: '高柜数量'
                        })}>{this.props.detail.highContainerCount}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.isLCL',
                            defaultMessage: '是否拼箱'
                        })}>{this.props.detail.isLCL ? Yes : No}</Description>
                        <Description term={formatMessage({
                            id: 'declare.text.remark',
                            defaultMessage: '备注'
                        })}>{this.props.detail.remark ? Yes : No}</Description>
                    </DescriptionList>
                </Card>

                <Card title={formatMessage({
                    id: 'reportPanel.title.declareAttachment',
                    defaultMessage: '报关附件清单'
                })}>
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FileUpload onChange={this.onChangeUpload} onRemove={this.props.removeAttachments} fileList={declareAttachments}/>
                        </Col>
                    </Row>
                </Card>

                <Card>
                    <Row>
                        <Col className="col-align-right">
                            <Button key="audit"
                                onClick={this.onSubmit}
                                type="primary"
                                loading={this.state.loading}>
                                {formatMessage({
                                    id: 'declarePanel.btn.save',
                                    defaultMessage: '保存'
                                })}</Button>
                        </Col>
                    </Row>
                </Card>
                
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'declare.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DeclarePanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    onCancel: PropTypes.func.isRequired,
    onDeclareSubmit: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    onDeclareSubmit,
    getDetail,
    tableSearch,
    updateAttachments,
    removeAttachments
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const getIsDealerUser = selectorFactory(['page', 'domainData', 'initData', 'isDealerUser']);

const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
    isDealerUser: getIsDealerUser(state)
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },

    onDeclareSubmit: (id, data) => dispatch(onDeclareSubmit(id, data)).then(res => {
        if(res.success) {
            dispatch(tableSearch());
            props.history.push(routes.query.url());
        }
    }),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    updateAttachments: data => dispatch(updateAttachments(data)),
    removeAttachments: ({id}) => dispatch(removeAttachments(id)),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DeclarePanel));
